package com.atxiao.demo.controller;

import com.atxiao.demo.bean.Account;
import com.atxiao.demo.bean.User;
import com.atxiao.demo.service.AccountService;
import com.atxiao.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    public UserService service;

    @Autowired
    private AccountService accountService;



    //跳转登陆页面
//    @RequestMapping({"/toLogin", "/"})

    @RequestMapping("/")
    public String toLogin() {
        return "index";
    }
    @RequestMapping("/main")
    public String toMainPage(HttpSession session,Model model) {
        User user = (User) session.getAttribute("user");
        System.out.println(session.getAttribute("phone"));
        model.addAttribute("user",user);

        List<Account> accounts = accountService.getAccountsByUser(user.getUserIdCard());
        model.addAttribute("cards",accounts);
        return "loginSuccess";

    }


    //用户登陆

    @RequestMapping("/login")
//   @ResponseBody
    public String userLogin(String phone, String password, Model model, HttpSession session,String code){

       String string = (String) session.getAttribute("code");

        if (!string.equalsIgnoreCase(code)) {
            model.addAttribute("msg","验证码错误");
            return "index";
        }
        //先进行用户名的判断，查看有没有这个用户
        User user1 = service.getUserByPhone(phone);

        if (user1==null){
            model.addAttribute("msg","用户名不存在");
            return "index";
        }else if(user1.getPassword()!=password){
            model.addAttribute("msg","密码错误");
        }

        User user = service.getUser(phone, password);
      System.out.println(user);
        if (user!=null){
            String time = getTime();        //获取登陆时间
            user.setStatus(1);
            user.setTime(time);
            // model.addAttribute("msg","登陆成功");
            model.addAttribute("user",user);    //存放用户信息
            System.out.println("============================");
            session.setAttribute("phone",phone);
            System.out.println("========================");
            service.changeStatus(1,user.getName(),time);     //修改登陆状态
            session.setAttribute("user",user);  //session中存放用户信息
            System.out.println(user);

            List<Account> accounts = accountService.getAccountsByUser(user.getUserIdCard());
            model.addAttribute("cards",accounts);

            return "loginSuccess";
        }
        return "index";

    }


    /**
     * 注销功能
     * @param session
     * @param request
     * @return
     */
    @RequestMapping("/logOut")
    public String logOut(HttpSession session, HttpServletRequest request) {
        String time = getTime();
        User user = (User) session.getAttribute("user");
        service.changeStatus(0, user.getName(), time); //修改状态
        session.removeAttribute("phone");
        session.removeAttribute("user");
        return "index";         //返回到登陆页面
    }
    //去注册页面

    /**
     * 打开注册页面
     * @return
     */
    @RequestMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

    //注册

    @PostMapping("/register")
    public String registerUser(String name, String password, String phone, Model model) {
       /* boolean flag = service.registerUser(name, password,phone);

        if (flag){
            model.addAttribute("msg","注册成功");
            return "/";
        }else {
            model.addAttribute("msg","注册有误");
        }*/
        service.registerUser(name, password, phone);
        return "redirect:/";


    }


//    //去存款界面
//    @RequestMapping("/deposit")
//    public String toDeposit() {
//        return "deposit";
//    }
    //去取款页面

    @RequestMapping("/transfer")
    public String toTransfer() {
        return "transfer";
    }

//    //去明细界面
//    @RequestMapping("/detail")
//    public String toDetail() {
//        return "detail";
//    }

    //操作成功后的跳转

    @RequestMapping("/success")
    public String success() {


        return "successPage";
    }
    //操作成功以后从操作成功的页面里面再去转到主页

    @RequestMapping("/reToMain")
    public String toIndex() {
        return "redirect:/main";
    }


    public static String getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        String time = sdf.format(date);
        return time;

    }
}


